package com.ljph.ljaker.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.StrKit;
import com.ljph.jfinal.base.BaseController;
import com.ljph.ljaker.app.AppConfig;
import com.ljph.ljaker.model.User;

/**
 * 用于处理cookie信息的拦截器
 * Created by yuzhou on 16/9/24.
 */
public class CookieInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {

        BaseController controller = (BaseController)inv.getController();
        User user = controller.getSessionAttr(AppConfig.SESSION_USER);

        if(user == null) {
            String userIdStr = controller.getCookie(AppConfig.COOKIE_USER);
            if(StrKit.notBlank(userIdStr)){
                user = User.dao.findById(userIdStr);
                controller.setSessionAttr(AppConfig.SESSION_USER, user);
            }
        }

        inv.invoke();

    }
}
